package gl.android.biyiqing.adapter;

import gl.android.biyiqing.R;
import gl.android.biyiqing.bean.BYQUserInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ShenFenAdapter extends BaseExpandableListAdapter {

	private Context context;
	private LayoutInflater mLayoutInflater;
	private OnSelect onSelect;

	public interface OnSelect {
		public void onselect(String gid, String gname, String childid,
				String cname);
	}

	View.OnClickListener click = new OnClickListener() {

		@Override
		public void onClick(View v) {
			final String string = v.getTag().toString();
			final String[] split = string.split(":");
			onSelect.onselect(split[0], split[1], split[2], split[3]);

		}
	};
	protected JSONArray dataJSONArray;

	public ShenFenAdapter(Context context, OnSelect onSelect) {
		this.context = context;
		mLayoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.onSelect = onSelect;
		this.dataJSONArray = BYQUserInfo.getShenfenJSON();
	}

	@Override
	public int getGroupCount() {

		return dataJSONArray.length();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		try {
			return dataJSONArray.getJSONObject(groupPosition)
					.getJSONArray("childs").length();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public Object getGroup(int groupPosition) {
		try {
			return dataJSONArray.getJSONObject(groupPosition);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		try {
			return dataJSONArray.getJSONObject(groupPosition)
					.getJSONArray("childs").getJSONObject(childPosition);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		GroupHolder groupHolder = null;
		if (convertView == null) {
			convertView = (View) getLayoutInflater().inflate(
					android.R.layout.simple_expandable_list_item_2, null);
			groupHolder = new GroupHolder();
			groupHolder.txt = (TextView) convertView
					.findViewById(android.R.id.text1);
			// groupHolder.img = (ImageView) convertView
			// .findViewById(R.id.img);
			convertView.setTag(groupHolder);
		}
		else {
			groupHolder = (GroupHolder) convertView.getTag();
		}
		groupHolder.txt.setText(((JSONObject) getGroup(groupPosition))
				.optString("name"));
		return convertView;
	}

	private LayoutInflater getLayoutInflater() {
		return mLayoutInflater;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ItemHolder itemHolder = null;
		convertView = (View) getLayoutInflater().inflate(
				android.R.layout.simple_expandable_list_item_1, null);
		itemHolder = new ItemHolder();
		itemHolder.txt = (TextView) convertView
				.findViewById(android.R.id.text1);
		convertView.setTag(itemHolder);
		itemHolder.txt.setText(((JSONObject) getChild(groupPosition,
				childPosition)).optString("name"));
		String tag = ((JSONObject) getGroup(groupPosition)).opt("id")
				+ ":"
				+ ((JSONObject) getGroup(groupPosition)).opt("name")
				+ ":"
				+ ((JSONObject) getChild(groupPosition, childPosition))
						.opt("id")
				+ ":"
				+ ((JSONObject) getChild(groupPosition, childPosition))
						.opt("name");
		itemHolder.txt.setTag(tag);
		itemHolder.txt.setOnClickListener(click);
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}

class GroupHolder {
	public TextView txt;
	public ImageView img;
}

class ItemHolder {
	public ImageView img;
	public TextView txt;
}
